package redis.client.nedis;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketTest {
    public static void main(String[] args) {
        try {
            //1.创建客户端Socket，指定服务器地址和端口
            Socket socket = new Socket("10.228.130.177", 7001);

            final String AUTH = "*2\r\n$4\r\nAUTH\r\n$12\r\nconfigMS7001\r\n";
            final String INFO = "*2\r\n$7\r\nCLUSTER\r\n$5\r\nNODES\r\n";
            final String GET = "*2\r\n$3\r\nGET\r\n$1\r\na\r\n";

            //2.获取输出流，向服务器端发送信息
            OutputStream os = socket.getOutputStream();

            //3.获取输入流，并读取服务器端的响应信息
            InputStream is = socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            os.write(AUTH.getBytes());
            System.out.println(br.readLine());
            os.write(INFO.getBytes());
            String n = br.readLine();
            int left = Integer.parseInt(n.substring(1));
            while (left > 0) {
                String s = br.readLine();
                System.out.println("left=" + left);
                System.out.println(s);
                left -= (s.length() + 1);
            }

            System.out.println("over");

            for (int i = 0; i < 0; i++) {

                long s = System.nanoTime();

                os.write(GET.getBytes());
                //socket.shutdownOutput();

                StringBuilder sb = new StringBuilder();
                sb.append(br.readLine());
                sb.append(br.readLine());
                sb.append(br.readLine());
                sb.append(br.readLine());
                sb.append(br.readLine());
                sb.append(br.readLine());


                //4.关闭资源
                //br.close();
                //is.close();

                long e = System.nanoTime();

                System.out.println("接收到服务器消息：" + sb.toString() + ";spend=" + (e - s) / 1e6 + "ms");
            }

            os.close();
            socket.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
